package com.zang.tree;

import java.util.*;

/**
 * @author ZVerify
 * @since 2022/10/30 20:44
 * @see <a href="https://leetcode.cn/problems/symmetric-tree/">对称二叉树</a>
 **/
public class 对称二叉树 {

    public boolean isSymmetric(TreeNode root) {

        if (root == null ) return true;
        boolean flag = true;
        Deque<TreeNode> linkedList = new LinkedList<>();
        linkedList.addLast(root);
        while (!linkedList.isEmpty()) {
            List<Integer> list = new ArrayList<>();
            for (int i = 0; i < linkedList.size(); i++) {
                TreeNode treeNode = linkedList.pollFirst();
                list.add(treeNode.val);
                if (treeNode.left!=null) linkedList.addLast(treeNode.left);
                if (treeNode.left!=null && treeNode.right==null) linkedList.addLast(new TreeNode(0));
                if (treeNode.left==null && treeNode.right!=null) linkedList.addLast(new TreeNode(0));
                if (treeNode.right!=null) linkedList.addLast(treeNode.right);
            }
            flag =  isTrue(list);
        }
        return true;
    }

    boolean isTrue(List<Integer> list){
        boolean flag = true;
        int[] ints = list.stream().mapToInt(x -> x).toArray();
        int left = 0,right = ints.length-1;
        while (left<right){
            if (ints[left] != ints[right]) {
                flag = false;
                break;
            }
            left++;
            right--;
        }
        return flag;
    }

}
